package com.vivi.feverdiagnose.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import com.vivi.feverdiagnose.db.DataSource;

import android.content.Context;
import android.util.Log;

public class JSONParser {
		private String url_gejala="http://10.0.2.2/service_pakar/getgejala.php";
		private String url_penyakit="http://10.0.2.2/service_pakar/getpenyakit.php";
		//private String url_role="http://10.0.2.2/service_pakar/role.php";
		//private String url_kuis="http://10.0.2.2:85/zoopedia/index.php/api/rest/allkuisformat/json";
		
		private DataSource mDataSource;
		
		public boolean JSONtoDBGejala(Context context, boolean update_db){
			String jsonGejala = getJSON(url_gejala);
			
			mDataSource = new DataSource(context);
	        mDataSource.open();
	        
			try {
				Log.d("Msg","JsonGejala = "+jsonGejala);
				
				JSONObject object = new JSONObject(jsonGejala);
				JSONArray allgejala = object.getJSONArray("gejala");
				for(int i = 0; i < allgejala.length(); i++){
					JSONObject data = allgejala.getJSONObject(i);
					
					String id = data.getString("idGejala");
					String nama = data.getString("nama");
					
					mDataSource.insertGejala(id, nama);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			mDataSource.close();
			return true;
		}
		
		public boolean JSONtoDBPenyakit(Context context, boolean update_db){
			String jsonPenyakit = getJSON(url_penyakit);
			
			mDataSource = new DataSource(context);
	        mDataSource.open();
	        
			try {
				Log.d("Msg","JsonPenyakit = "+jsonPenyakit);
				
				JSONObject object = new JSONObject(jsonPenyakit);
				JSONArray allpenyakit = object.getJSONArray("penyakit");
				for(int i = 0; i < allpenyakit.length(); i++){
					JSONObject data = allpenyakit.getJSONObject(i);
					
					String id = data.getString("idPenyakit");
					String nama = data.getString("nama");
					String saran = data.getString("saran");
					
					mDataSource.insertPenyakit(id, nama, saran);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			mDataSource.close();
			return true;
		}
		
		

		public String getJSON(String url) {
			StringBuilder builder = new StringBuilder();
			HttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			try {
				HttpResponse response = client.execute(httpGet);
				StatusLine statusLine = response.getStatusLine();
				int statusCode = statusLine.getStatusCode();
				if (statusCode == 200) {
					HttpEntity entity = response.getEntity();
					InputStream content = entity.getContent();
					BufferedReader reader = new BufferedReader(new InputStreamReader(content));
					String line;
					while ((line = reader.readLine()) != null) {
						builder.append(line);
					}
				} else {
					Log.e("Get JSON", "Failed to get JSON from the link.");
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return builder.toString();
		}
}
